const path = require("path");

const prog = require("commander");
const { promisify } = require("util");
const downGit = promisify(require("download-git-repo"));
// 进度条
const Bar = require("../utils/progress");
const terminal = require("../utils/terminal");
const configManager = require("../utils/configRW");
const { compileEJS, writeEJS } = require("../utils/compile");

function handleOptions(keys, callback) {
	let options = prog.program.opts();
	let values = [];
	if (typeof keys === "string") {
		// 选项是否选中,没选中return
		if (!Object.keys(options).includes(keys)) return;
		values = options[keys];
	} else if (keys instanceof Array) {
		for (let key of keys) {
			// 选项是否选中,没选中处理下一个选项
			if (!Object.keys(options).includes(keys)) continue;
			values.push(options[key]);
		}
		//一个都没选中直接rerun
		if (values === []) {
			return;
		}
	}

	return callback(values);
}

async function createAction(project) {
	// 进度条
	Bar.start();
	// 参数处理
	handleOptions("setTempOrigin", (setTempOrigin) => {
		configManager.set(configManager.KEY.ORIGIN, `direct:${setTempOrigin}`);
	});
	// 从远程拉取模板
	await downGit(configManager.get(configManager.KEY.ORIGIN), project, {
		clone: true,
	});
	// 安装依赖包
	await terminal.command("npm", ["install"], { cwd: path.resolve(project) });
	Bar.end("下载完成!正在启动项目.....");
	// 运行项目
	await terminal.command("npm", ["run", "serve"], {
		cwd: path.resolve(project),
	});
}

async function addPageAction(pageName) {
	// 处理参数
	handleOptions("dest", (dest) => {
		configManager.set(configManager.KEY.PAGEDEST, dest);
	});
	let name = { name: pageName, lower: pageName.toLowerCase() };
	// 编译模板
	const cmps = await compileEJS("lib/temp/cpns.vue.ejs", name);
	const router = await compileEJS("lib/temp/router.js.ejs", name);
	// 取出路径
	const filePath = configManager.get(configManager.KEY.PAGEDEST);

	// 写入文件
	writeEJS(filePath, `${pageName}.vue`, cmps);
	writeEJS(path.join(filePath, pageName), "router.js", router, false);
}

async function addStore(storeName) {
	handleOptions("dest", (dest) => {
		configManager.set(configManager.KEY.STOREDEST, dest);
	});
	//编译模板
	const store = await compileEJS("lib/temp/store.js.ejs", null);
	const types = await compileEJS("lib/temp/types.js.ejs", null);
	// 取出路径
	const filePath = configManager.get(configManager.KEY.STOREDEST);
	// 写入文件
	writeEJS(filePath, `${storeName}.js`, store);
	writeEJS(path.join(filePath, storeName), `types.js`, types, false);
}

async function addComponents(cpnsName) {
	// 处理参数
	handleOptions("dest", (dest) => {
		configManager.set(configManager.KEY.CPNSDEST, dest);
	});
	// 编译
	const name = { name: cpnsName, lower: cpnsName.toLowerCase() };
	const cpns = await compileEJS("lib/temp/cpns.vue.ejs", name);
	// 读取路径
	const filepath = configManager.get(configManager.KEY.CPNSDEST);
	// 写入文件
	writeEJS(filepath, `${cpnsName}.vue`, cpns);
}
function showConfig() {
	const config = configManager.getAll();
	console.log("-------------------------------------");
	console.log("配置信息:");
	for (let key of Object.keys(config)) {
		console.log(`  ${key} : ${config[key]}`);
	}
	console.log("-------------------------------------");
}

module.exports = {
	createAction,
	addPageAction,
	addStore,
	addComponents,
	showConfig,
};
